package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.common.dto.ListResult;
import com.mysoft.gfyx.common.time.TimeService;
import com.mysoft.gfyx.marketdevelop.dao.entity.OpTelViewHistory;
import com.mysoft.gfyx.marketdevelop.mip.GetSupplierDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.GetSupplierContactRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.GetSupplierProjectRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import com.mysoft.gfyx.marketdevelop.service.ProvinceRadarService;
import com.mysoft.gfyx.marketdevelop.service.SupplierDetailTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SupplierDetailTableServiceImp extends AppService implements SupplierDetailTableService {
    @Autowired
    private GetSupplierDataService getSupplierDataService;

    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    @Autowired
    private TimeService timeService;

    @Resource
    private ProvinceRadarService provinceRadarService;

    public FieldsSetting loadFieldsSupplierProject() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "supplier_project";
        fieldsSetting.getFields().add(new DataApiField("supplier_id", groupName, "工程单位id", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_name", groupName, "工程单位名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "工程单位信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_prj_name", groupName, "合作项目名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_project_type", groupName, "合作模式", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_start_time", groupName, "合作开始时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_end_time", groupName, "合作结束时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_company_name", groupName, "合作单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_company_code", groupName, "合作单位信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("data_source", groupName, "数据来源", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_province_code", groupName, "项目省份code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_province", groupName, "项目省份", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_city_code", groupName, "项目城市code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_city", groupName, "项目省市", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_amount", groupName, "合作金额", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("coop_amount_orgin", groupName, "合作金额原始值", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataSupplierProject(LoadDataParams options) {
        if (options.getOptions().getContext().get("organizing_code") == null || options.getOptions().getContext().get("organizing_code").toString().equals("")) {
            return new ListDataResult();
        }
        GetSupplierProjectRequestDto dto = new GetSupplierProjectRequestDto();
        dto.setOrganizing_code(options.getOptions().getContext().get("organizing_code").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getSupplierDataService.getSupplierProject(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            p.put("id", UUID.randomUUID().toString());
            if (p.get("coop_start_time") != null && !p.get("coop_start_time").equals(""))
                p.put("coop_start_time", p.get("coop_start_time").toString().substring(0, 10));
            if (p.get("coop_end_time") != null && !p.get("coop_end_time").equals(""))
                p.put("coop_end_time", p.get("coop_end_time").toString().substring(0, 10));

            if (p.get("coop_amount") == null || p.get("coop_amount").equals("")) {
                p.put("coop_amount", 0);
            }
            double tmp = Double.parseDouble(p.get("coop_amount").toString());
            BigDecimal four = new BigDecimal(tmp);
            p.put("coop_amount", four.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());

        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsSupplierContact() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "supplier_contact";
        fieldsSetting.getFields().add(new DataApiField("supplier_id", groupName, "工程单位id", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_name", groupName, "工程单位名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "工程单位信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_user_name", groupName, "联系人姓名", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_position", groupName, "联系人职位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_phone", groupName, "联系人电话", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_phone_type", groupName, "联系人电话类型", FieldType.String));//枚举值：手机、座机
        fieldsSetting.getFields().add(new DataApiField("contact_create_time", groupName, "创建时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_source", groupName, "联系人来源", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("source_id", groupName, "联系人来源id", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_source_link", groupName, "联系人来源链接", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_province", groupName, "联系人省", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_province_code", groupName, "联系人省code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_city", groupName, "联系人市", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_city_code", groupName, "联系人市code", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("first_name", groupName, "姓", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("create_period", groupName, "最近更新时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("source_num", groupName, "联系人来源编号", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("source_title", groupName, "联系人来源标题", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataSupplierContact(LoadDataParams options) {
        if (options.getOptions().getContext().get("organizing_code") == null || options.getOptions().getContext().get("organizing_code").toString().equals("")) {
            return new ListDataResult();
        }
        GetSupplierContactRequestDto dto = new GetSupplierContactRequestDto();
        dto.setOrganizing_code(options.getOptions().getContext().get("organizing_code").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getSupplierDataService.getSupplierContact(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("contact_create_time") != null && !p.get("contact_create_time").equals("") && p.get("contact_create_time").toString().length() >= 10) {
                p.put("create_period", timeService.getLatestPeriod(p.get("contact_create_time")) + "更新");
                p.put("contact_create_time", p.get("contact_create_time").toString().substring(0, 10));
            } else {
                p.put("create_period", "");
            }

            if (p.get("contact_user_name") != null && !p.get("contact_user_name").equals("") && p.get("contact_user_name").toString().length() >= 1) {
                p.put("first_name", p.get("contact_user_name").toString().substring(0, 1));
            }

            String phone = p.get("contact_phone").toString();
            //手机号码查看次数
            if (!phone.equals("")) {
                Set<String> mobileSet = new HashSet<>();
                mobileSet.add(phone);
                Map<String, OpTelViewHistory> getTelViewHistories = provinceRadarService.getTelViewHistories(mobileSet);
                if (getTelViewHistories.size() > 0) {
                    p.put("phone_view_times", getTelViewHistories.get(phone).getViewTimes());
                } else {
                    p.put("phone_view_times", 0);
                }
            }

        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }


}
